$(function(){
    /*======================================================
    - statInput 输入限制统计
    - 长度超出_max的话就截取...貌似没有更好的办法了
    ======================================================*/
    function statInput(e, _max, _exp) {
        var my_e = $2(e);
        for (var j = 0; j < my_e.length; j++) {
            myStatInput(my_e[j], _max, _exp);
        }
    }

    function myStatInput(e, _max, _exp) {
        _max = parseInt(_max);
        _max = isNaN(_max) ? 0 : _max;
        _exp = _exp == undefined ? {} : _exp;
        if (e == null || _max == 0) {
            alert('statInput初始化失败！');
            return;
        }
        // 浏览器
        var _brower = brower();
        // 输出对象
        var _objMax = _exp._max == undefined ? null : $("#"+_exp._max);
        var _objTotal = _exp._total == undefined ? null : $("#"+_exp._total);
        var _objLeft = _exp._left == undefined ? null : $("#"+_exp._left);
        var _hint= _exp._hint==undefined ?null : _exp._hint;
        // 初始统计
        if (_objMax != null) {
            _objMax.innerHTML = _max;
        }
        if (_objTotal != null) {
            _objTotal.innerHTML = 0;
        }
        if (_objLeft != null) {
            _objLeft.innerHTML = 0;
        }

        // 设置监听事件
        // 输入这个方法比较好.
        // 但是Opera下中文输入跟粘贴不能正确统计...相当BT的东西...
        // 如果不考虑Opera的话就用这个吧.否则就老老实实用计时器.
        if (_brower.isIE) {
            myAddEventListener(e, "propertychange", stat);
        } else {
            myAddEventListener(e, "input", stat);
        }

        // 统计函数
        var _len, _olen, _lastRN, _sTop;
        _olen = _len = 0;

        function stat() {
            _len = e.value.length;
            // 防止用计时器监听时做无谓的牺牲...
            if (_len == _olen) {
                return;
            }

            if (_len > _max) {
                _sTop = e.scrollTop;
                // 避免IE最后俩字符为'\r\n'.导致崩溃...
                _lastRN = (e.value.substr(_max - 1, 2) == "\r\n");
                e.value = e.value.substr(0, (_lastRN ? _max - 1 : _max));
                /*if (_hint == true) {
                    HFalert({title:"悟空你也太调皮了，为师跟你说过，叫你不要输那么多字~~.", type: 'warning', confirmButtonText: '确定'});
                }*/
                // 解决FF老是跑回顶部
                if (_brower.isFirefox) {
                    e.scrollTop = e.scrollHeight;
                }
            }
            _olen = _len = e.value.length;

            // 显示已输入字数
            if (_objTotal != null) {
                _objTotal.innerHTML = _len;
            }

            // 显示剩余可输入字数
            if (_objLeft != null) {
                _objLeft.innerHTML = (_max - _len) < 0 ? 0 : (_max - _len);
            }
        }

        stat();
    }


    function getElementByClass(clsName) {
        //取得页面中所有元素
        var result = [];
        var allEle = document.getElementsByTagName("*");
        //循环每一个元素
        for (var i = 0; i < allEle.length; i++) {
            //class 是保留字  所以不能直接用class  用classname
            var arr = allEle[i].className.split(" ");
            for (var k = 0; k < arr.length; k++) {
                if (arr[k] == clsName) {
                    result.push(allEle[i]);
                }
            }
        }
        return result;
    }

    // 这里都是公用函数，挺多的...
    // 获取元素
    var $2 = function (element) {
        return (typeof (element) == 'object' ? element : getElementByClass(element));
    },
    // 判断浏览器
    brower = function () {
        var ua = navigator.userAgent.toLowerCase();
        var os = new Object();
        os.isFirefox = ua.indexOf('gecko') != -1;
        os.isOpera = ua.indexOf('opera') != -1;
        os.isIE = !os.isOpera && ua.indexOf('msie') != -1;
        os.isIE7 = os.isIE && ua.indexOf('7.0') != -1;
        return os;
    },
    // 事件操作(可保留原有事件)
    eventListeners = [],
    findEventListener = function (node, event, handler) {
        var i;
        for (i in eventListeners) {
            if (eventListeners[i].node == node && eventListeners[i].event == event && eventListeners[i].handler == handler) {
                return i;
            }
        }
        return null;
    },
    // 输入响应
    myAddEventListener = function (node, event, handler) {
        if (findEventListener(node, event, handler) != null) {
            return;
        }
        if (!node.addEventListener) {
            node.attachEvent('on' + event, handler);
        } else {
            node.addEventListener(event, handler, false);
        }
        eventListeners.push({node: node, event: event, handler: handler});
    };

    myAddEventListener(window, "load", testStatInput());

    function testStatInput() {
        statInput('Test_1', 100, {_max: 'stat_max', _total: 'stat_total', _left: 'stat_left', _hint: true});
    }
});